/*
 * @Description: 待办接口modal
 * @Author: wws1993
 * @Date: 2020-11-26 14:02:01
 * @LastEditTime: 2020-12-14 11:10:01
 * @LastEditors: wws1993
 * @FilePath: \TODO-MANAGER\src\service\TodoManageModal.ts
 */

import BaseModal from './BaseModal';

class TodoManageModal extends BaseModal {
  constructor() {
    super('TODOMANAGES');
  }

  /** 保存管理器实例
   * @param params 表单
   */
  public save(params: Manage.Instance) {
    return params.id
      ? this.update(params)
      : this.create(params);
  }

  /** 查 */
  public request(params?: API.params.TodoManageModal.Query): Promise<Manage.Instance[] | null> {
    return new Promise((resolve, reject) => {
      this.query().then(res => {
        res = res || [];
        if (params && Object.keys(params).length !== 0) {
          let result = [...res] as Manage.Instance[];

          if (params.subject) {
            result = result.filter(k => k.subject.indexOf(params.subject as string) !== -1);
          }

          if (params.weight) {
            result = result.filter(k => k.weight === params.weight);
          }

          if (params.startTime) {
            result = result.filter(k => new Date(k.startTime).getTime() >= (params.startTime as Date).getTime());
          }

          if (params.endTime) {
            result = result.filter(k => new Date(k.endTime).getTime() <= (params.endTime as Date).getTime());
          }

          if (params.userIds) {
            params.userIds.split(',').map(user => {
              result = result.filter(k => k.person.join(',').indexOf(user) !== -1);
            });
          }

          resolve(result);
        } else {
          resolve(res as Manage.Instance[]);
        }
      });
    });
  }

  public del(id: number|number[]) {
    if (typeof id === 'number') {
      return this.delete(id);
    } else {
      const requests = id.map(k => this.delete(k));

      return Promise.all(requests);
    }
  }
}

export default new TodoManageModal();
